package com.leecode.exercise;

/**
 * @Author: shubin_yao
 * @Date: 2022/09/18/9:13
 * @Description: 5. 最长回文子串
 * <p>
 * 输入：s = "babad"
 * 输出："bab"
 * 解释："aba" 同样是符合题意的答案。
 */
public class LeeCode_5_Palindrome {

    public static void main(String[] args) {
        String s = "babad";
        LeeCode_5_Palindrome palindrome = new LeeCode_5_Palindrome();
        System.out.println(palindrome.longestPalindrome(s));
    }


    public String longestPalindrome(String s) {
        int len = s.length();
        String str = "";
        int max = 0;
        for (int i = 0; i < len; i++) {
            for (int j = i + 1; j <= len; j++) {
                String s1 = s.substring(i, j);
                if(isPalindrome(s1) && max < s1.length()){
                    str = s1;
                    max = Math.max(max,s1.length());
                }
            }
        }
        return str;
    }


    //判断是不是回文字串
    public boolean isPalindrome(String s) {
        int len = s.length();
        for (int i = 0; i < len / 2; i++) {
            if (s.charAt(i) != s.charAt(len - 1 - i)) {
                return false;
            }
        }
        return true;
    }


}
